package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.membershipplan.MembershipPlanQueryRequest;
import com.yupi.yuoj.model.entity.MembershipPlan;
import com.yupi.yuoj.model.vo.MembershipPlanVO;
import com.yupi.yuoj.service.MembershipPlanService;
import com.yupi.yuoj.mapper.MembershipPlanMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【membership_plan(会员套餐表)】的数据库操作Service实现
* @createDate 2025-11-01 21:28:38
*/
@Service
public class MembershipPlanServiceImpl extends ServiceImpl<MembershipPlanMapper, MembershipPlan>
    implements MembershipPlanService{

    @Override
    public void validMembershipPlan(MembershipPlan membershipPlan, boolean add) {
        if (membershipPlan == null) {
            throw new RuntimeException("会员套餐不能为空");
        }
        
        String planName = membershipPlan.getPlan_name();
        Integer duration = membershipPlan.getDuration();
        BigDecimal price = membershipPlan.getPrice();
        
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(planName), ErrorCode.PARAMS_ERROR, "套餐名称不能为空");
            ThrowUtils.throwIf(duration == null || duration <= 0, ErrorCode.PARAMS_ERROR, "套餐时长不能为空且必须大于0");
            ThrowUtils.throwIf(price == null || price.compareTo(BigDecimal.ZERO) <= 0, ErrorCode.PARAMS_ERROR, "套餐价格不能为空且必须大于0");
        }
        
        // 校验套餐名称长度
        if (StringUtils.isNotBlank(planName) && planName.length() > 50) {
            throw new RuntimeException("套餐名称过长");
        }
        
        // 校验时长范围
        if (duration != null && (duration < 1 || duration > 120)) {
            throw new RuntimeException("套餐时长必须在1-120个月之间");
        }
        
        // 校验价格范围
        if (price != null && (price.compareTo(BigDecimal.ZERO) <= 0 || price.compareTo(new BigDecimal("99999.99")) > 0)) {
            throw new RuntimeException("套餐价格必须在0.01-99999.99之间");
        }
    }

    @Override
    public QueryWrapper<MembershipPlan> getQueryWrapper(MembershipPlanQueryRequest membershipPlanQueryRequest) {
        QueryWrapper<MembershipPlan> queryWrapper = new QueryWrapper<>();
        if (membershipPlanQueryRequest == null) {
            return queryWrapper;
        }
        
        Long id = membershipPlanQueryRequest.getId();
        String name = membershipPlanQueryRequest.getName();
        String type = membershipPlanQueryRequest.getType();
        BigDecimal minPrice = membershipPlanQueryRequest.getMinPrice();
        BigDecimal maxPrice = membershipPlanQueryRequest.getMaxPrice();
        Boolean isRecommended = membershipPlanQueryRequest.getIsRecommended();
        Integer status = membershipPlanQueryRequest.getStatus();
        String sortField = membershipPlanQueryRequest.getSortField();
        String sortOrder = membershipPlanQueryRequest.getSortOrder();
        
        // 拼接查询条件
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "plan_name", name);
        // type字段在数据库表中不存在，暂时注释掉
        // queryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        queryWrapper.ge(minPrice != null && minPrice.compareTo(BigDecimal.ZERO) >= 0, "price", minPrice);
        queryWrapper.le(maxPrice != null && maxPrice.compareTo(BigDecimal.ZERO) >= 0, "price", maxPrice);
        queryWrapper.eq(isRecommended != null, "is_popular", isRecommended);
        queryWrapper.eq(status != null, "is_active", status);
        
        // 排序规则
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals("ascend"), sortField);
        
        return queryWrapper;
    }

    @Override
    public MembershipPlanVO getMembershipPlanVO(MembershipPlan membershipPlan, HttpServletRequest request) {
        MembershipPlanVO membershipPlanVO = MembershipPlanVO.objToVo(membershipPlan);
        return membershipPlanVO;
    }

    @Override
    public Page<MembershipPlanVO> getMembershipPlanVOPage(Page<MembershipPlan> membershipPlanPage, HttpServletRequest request) {
        List<MembershipPlan> membershipPlanList = membershipPlanPage.getRecords();
        Page<MembershipPlanVO> membershipPlanVOPage = new Page<>(membershipPlanPage.getCurrent(), membershipPlanPage.getSize(), membershipPlanPage.getTotal());
        if (membershipPlanList.isEmpty()) {
            return membershipPlanVOPage;
        }
        
        // 填充信息
        List<MembershipPlanVO> membershipPlanVOList = membershipPlanList.stream()
                .map(membershipPlan -> getMembershipPlanVO(membershipPlan, request))
                .collect(Collectors.toList());
        
        membershipPlanVOPage.setRecords(membershipPlanVOList);
        return membershipPlanVOPage;
    }
}




